﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using InventorySys.controller;
using Model.Upadan;
using Model.Sadharon;
using Model.Sohaok;
namespace InventorySys.view
{
    public partial class Customize : Form
    {
        #region private variable
        /// <summary>
        /// private variable
        /// </summary>
        private SizeController sizeControl;
        private ColorController colorControl;
        private SaleTypeController saleTypeController;
        private ProductColor color;
        private ProductSize size;
        private SaleType saleType;
        private Result result;
        private List<ProductSize> allSize;
        private List<ProductColor> allColor;
        private List<SaleType> allSaleType;
        private string sizeName;
        private string colorName;

        private ListViewItem item;
        
        #endregion

        #region constructor
        /// <summary>
        /// constructor
        /// </summary>
        public Customize()
        {
            InitializeComponent();
            sizeControl = new SizeController();
            colorControl = new ColorController();
            saleTypeController = new SaleTypeController();
            color = new ProductColor();
            size = new ProductSize();
            saleType = new SaleType();
        }
        #endregion

        #region page loading functions
        /// <summary>
        /// page loading functions
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Customize_Load(object sender, EventArgs e)
        {
            populateColorData();
            populateSizeData();
            populateSaleTypeData();
        }
        #endregion

        #region add size function
        /// <summary>
        /// add size function
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Add_Size_Click(object sender, EventArgs e)
        {
            try
            {
                size.Name = PR_Size.Text;
                size.isLive = 'L';
                if (sizeControl.addNewSize(size).IsSuccess)
                {
                    populateSizeData();
                }
                else
                {
                    MessageBox.Show("Failed to add Size");
                }
            }
            catch { }
        }
        #endregion

        #region add color function
        /// <summary>
        /// add color function
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Add_Color_Click(object sender, EventArgs e)
        {
            try
            {
                color.Name = PR_Color.Text;
                color.isLive = 'L';
                if (colorControl.addNewColor(color).IsSuccess) { }
                else
                {
                    MessageBox.Show("Failed to new Colour");
                }
            }
            catch { }
        }
        #endregion

        #region populate all size data
        /// <summary>
        /// populate all size data
        /// </summary>
        private void populateSizeData()
        {
            PR_SizeList.Items.Clear();
            allSize = (List<ProductSize>)sizeControl.GetAllProductSize().ResultObj;
            foreach (ProductSize _size in allSize)
            {
                PR_SizeList.Items.Add(_size.Name);
            }
        }
        #endregion

        #region populate all color data
        /// <summary>
        /// populate all color data
        /// </summary>
        private void populateColorData()
        {
            PR_ColorList.Items.Clear();
            allColor = (List<ProductColor>)colorControl.GetAllProductColor().ResultObj;
            foreach (ProductColor _color in allColor)
            {
                PR_ColorList.Items.Add(_color.Name);
            }
        }
        #endregion

        #region populate all sale type data
        /// <summary>
        /// populate all sale type data
        /// </summary>
        private void populateSaleTypeData()
        {
            allSaleType = (List<SaleType>)saleTypeController.GetAllSaleType().ResultObj;
            ListBoxSalType.Items.Clear();
            if (allSaleType != null)
            {
                foreach (SaleType _saleType in allSaleType)
                {
                    ListBoxSalType.Items.Add(new Item(_saleType.Name, _saleType.Id));
                }
            }
        }
        #endregion

        #region PR_SizeList_SelectedValueChanged
        /// <summary>
        /// PR_SizeList_SelectedValueChanged
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PR_SizeList_SelectedValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (PR_SizeList.Items != null)
                {
                    PR_Size.Text = PR_SizeList.SelectedItem.ToString();
                    sizeName = PR_Size.Text.ToString();
                }
            }catch(Exception ex){
            }
        }
        #endregion

        #region PR_ColorList_SelectedValueChanged
        /// <summary>
        /// PR_ColorList_SelectedValueChanged
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PR_ColorList_SelectedValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (PR_ColorList.Items != null)
                {
                    PR_Color.Text = PR_ColorList.SelectedItem.ToString();
                    colorName = PR_Color.Text.ToString();
                }
            }catch(Exception exx){
            }
        }
        #endregion

        #region size modify button
        /// <summary>
        /// size modify button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PR_SizeModify_Click(object sender, EventArgs e)
        {
            try
            {
                ModifySize();
            }
            catch { }
        }
        #endregion

        #region modify size
        /// <summary>
        /// modify size
        /// </summary>
        private void ModifySize() {
            Result result = sizeControl.UpdateProductSize(PR_Size.Text.ToString(), sizeName);
            size = (ProductSize)result.ResultObj;
            if (result.IsSuccess)
            {
                PR_Size.Text = size.Name;
                PR_SizeList.Items.Clear();
                populateSizeData();
            }
            else
            {
                MessageBox.Show("Failed to update");
            }

        }
        #endregion

        #region  PR_ColorModify_Click
        /// <summary>
        /// PR_ColorModify_Click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PR_ColorModify_Click(object sender, EventArgs e)
        {
            try
            {
                ModifyColor();
            }
            catch { }
        }
        #endregion

        #region modify color
        /// <summary>
        /// modify color
        /// </summary>
        private void ModifyColor() {

            Result result = colorControl.UpdateProductColor(PR_Color.Text.ToString(), colorName);
            color = (ProductColor)result.ResultObj;
            if (result.IsSuccess) {
                PR_Color.Text = color.Name;
                PR_ColorList.Items.Clear();
                populateColorData();
            }
            else
            {
                MessageBox.Show("Failed to modify colour");
            }
        }
        #endregion

        #region delete color
        /// <summary>
        /// delete color
        /// </summary>
        private void DeleteColor() {
            if (colorControl.DeleteProductColor(colorName).IsSuccess)
            {
                PR_Color.Text = "";
                PR_ColorList.Items.Clear();
                populateColorData();
            }
            else
            {
                MessageBox.Show("Failed to delete");
            }
        }
        #endregion

        #region delete size
        /// <summary>
        /// delete size
        /// </summary>
        private void DeleteSize() {
            if (sizeControl.DeleteProductSize(sizeName).IsSuccess)
            {
                PR_Size.Text = "";
                PR_SizeList.Items.Clear();
                populateSizeData();
            }
            else
            {
                MessageBox.Show("Failed to delete");
            }
        }
        #endregion

        #region PR_SizeDelete_Click
        /// <summary>
        /// PR_SizeDelete_Click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PR_SizeDelete_Click(object sender, EventArgs e)
        {
            try
            {
                DeleteSize();
            }
            catch { }
        }
        #endregion

        #region PR_ColorDelete_Click
        /// <summary>
        /// PR_ColorDelete_Click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PR_ColorDelete_Click(object sender, EventArgs e)
        {
            try
            {
                DeleteColor();
            }
            catch { }
        }
        #endregion

        #region add new sale type
        /// <summary>
        /// add new sate type
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonAddSaleType_Click(object sender, EventArgs e)
        {
            saleType.Name = TextBoxSaleType.Text;
            saleType.Live = "L";
            if (saleType.Name.Length > 0)
            {
                result = saleTypeController.addSaleTypeDetails(saleType);
                if (result.IsSuccess)
                {
                    TextBoxSaleType.Text = "";
                    TextBoxSaleTypeIdHidden.Text = "";
                    populateSaleTypeData();
                }
                else
                {
                    MessageBox.Show(result.Message);
                }
            }
        }
        #endregion

        #region modify sale type
        /// <summary>
        /// modify sale type
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonModifySaleType_Click(object sender, EventArgs e)
        {
            saleType = new SaleType();
            saleType.Id = TextBoxSaleTypeIdHidden.Text;
            saleType.Name = TextBoxSaleType.Text;
            saleType.Live = "L";
            if (saleType.Id.Length > 0)
            {
                result = saleTypeController.updateSaleTypeDetails(saleType);
                if (result.IsSuccess)
                {
                    TextBoxSaleType.Text = "";
                    TextBoxSaleTypeIdHidden.Text = "";
                    populateSaleTypeData();
                }
                else
                {
                    MessageBox.Show(result.Message);
                }
            }
        }
        #endregion

        #region delete sale type
        /// <summary>
        /// delete sale type
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonSaleTypeDelete_Click(object sender, EventArgs e)
        {
            string id = TextBoxSaleTypeIdHidden.Text;
            if (id.Length > 0)
            {
                result = saleTypeController.deleteSaleTypeDetails(id);
                if (result.IsSuccess)
                {
                    TextBoxSaleType.Text = "";
                    TextBoxSaleTypeIdHidden.Text = "";
                    populateSaleTypeData();
                }
                else
                {
                    MessageBox.Show(result.Message);
                }
            }
        }
        #endregion

        #region list box sale type selected value changed
        /// <summary>
        /// list box sale type selected value changed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ListBoxSalType_SelectedValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (ListBoxSalType.Items != null)
                {
                    Item item = (Item)ListBoxSalType.SelectedItem;
                    TextBoxSaleType.Text = item.ToString();
                    TextBoxSaleTypeIdHidden.Text = item.getValue();                    
                }
            }
            catch (Exception ex)
            {
            }
        }
        #endregion


    }
}
